<?php
class blogdao extends daobase {
	public function goGetBlogKind($userId){
		$sql = "select content,id from blogkind where userId ='$userId' order by id asc";
		$kinds = $this->fetchit($sql);
		return $kinds;
	
	}
	public function goInsertNewBlogKind($userId,$kind){
		$sql ="insert into blogkind(userId,content) values('$userId','$kind')";
		$query = $this->queryit($sql);
		return $query;
	}
	public function goAddNewBlog($title,$kind,$content,$postdate,$userId,$quanxian){
		$sql ="insert into blog(title,kind,content,postTime,userId,quanxian) values('$title','$kind','$content','$postdate','$userId','$quanxian')";
		//echo $sql;
		$query = $this->queryit($sql);
		if ($query){
			$sql = "select id from blog where title ='$title' and userId = '$userId' and postTime ='$postdate'";
			$newAddBlogId = $this->fetchit($sql);
			$newAddBlogId = $newAddBlogId[0][0];
		}else {
			$newAddBlogId = $query;
		}
		return $newAddBlogId;
	}
	public function goGetBlog($blogId){
		$sql = "select * from blog where id ='$blogId'";
		$blog = $this->fetchit($sql);
		return $blog;
	}
	public function goUpdateBlog($title,$kind,$content,$postdate,$userId,$quanxian,$blogId){
		$sql ="select updateTime from blog where id='$blogId'";
		$updateDates = $this->fetchit($sql);
		$updateDates=$updateDates[0][0];
		if($updateDates!=""){
			$updateDates.=",".$postdate;
		}
		else {
			$updateDates=$postdate;
		}
		$sql = "update blog set title ='$title',kind='$kind',content='$content',updateTime='$updateDates',userId='$userId',quanxian='$quanxian' where id='$blogId'";
		//echo $sql;
		$query = $this->queryit($sql);
		return $query;
	}
	public function goAddBlogDraft($title,$kind,$content,$postdate,$userId,$quanxian,$draftId){
		if($draftId){
			$sql = "select id from blogDraft where id ='$draftId'";
			$check = $this->fetchit($sql);
			if ($check[0][0]){
				$sql = "update blogDraft SET title='$title',kind ='$kind',content='$content',postTime='$postdate',userId='$userId',quanxian='$quanxian' where id ='$draftId'";
			}else{
				$sql ="insert into blogDraft(title,kind,content,postTime,userId,quanxian) values('$title','$kind','$content','$postdate','$userId','$quanxian')";
			}
		}else{
			$sql ="insert into blogDraft(title,kind,content,postTime,userId,quanxian) values('$title','$kind','$content','$postdate','$userId','$quanxian')";
		}
		//echo $sql;
		$query = $this->queryit($sql);
		if ($query){
			$newAddBlogDraftId =$this->goGetTargetDraftId($userId);
		}else {
			$newAddBlogDraftId = $query;
		}
		return $newAddBlogDraftId;
	}
	public function goReadBlogDraft($userid){
		$sql = "select * from blogDraft where userId='$userid' order by postTime desc";
		$draft = $this->fetchit($sql);
		return $draft;
	}
	public function goGetBlogDraftId($userId){
		$sql = "SELECT count(id) FROM `blogDraft` WHERE userId = '$userId'";
		$draftId = $this->fetchit($sql);
		$draftId = $draftId[0][0];
		return $draftId;
	}
	public function goGetTargetDraftId($userId){
		$sql = "SELECT id FROM `blogDraft` WHERE userId = '$userId'";
		$draftId = $this->fetchit($sql);
		$draftId = $draftId[0][0];
		return $draftId;
	}
	public function goReadTargetDraft($draftid){
		$sql = "SELECT * FROM `blogDraft` WHERE id = '$draftid'";
		$draft = $this->fetchit($sql);
		return $draft;
	}
	public function goDeleteTargetDraft($draftid){
		$sql = "delete FROM `blogDraft` WHERE id = '$draftid'";
		$delete = $this->queryit($sql);
		return $delete;
	}
	public function getTargetBlogContent($blogId){
		$sql = "select content from blog where id ='$blogId'";
		$content = $this->fetchit($sql);
		$content = $content[0][0];
		return $content;
	}
	

}